package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.CommonFeedback;
import com.ktgj.customer.service.ICommonFeedbackService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 意见反馈Controller
 * 
 * @author ruoyi
 * @date 2021-11-22
 */
@ControllerAdvice
@RestController
@RequestMapping("/feedback")
public class CommonFeedbackController extends BaseController
{
    @Autowired
    private ICommonFeedbackService commonFeedbackService;

    @Autowired
    private ILoginService loginService;

    /**
     * 查询意见反馈列表
     */
    @PreAuthorize(hasPermi = "customer:feedback:list")
    @GetMapping("/list")
    public TableDataInfo list(CommonFeedback commonFeedback)
    {
        startPage();
        List<CommonFeedback> list = commonFeedbackService.selectCommonFeedbackList(commonFeedback);
        return getDataTable(list);
    }

    /**
     * 导出意见反馈列表
     */
    @PreAuthorize(hasPermi = "customer:feedback:export")
    @Log(title = "意见反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommonFeedback commonFeedback) throws IOException
    {
        List<CommonFeedback> list = commonFeedbackService.selectCommonFeedbackList(commonFeedback);
        ExcelUtil<CommonFeedback> util = new ExcelUtil<CommonFeedback>(CommonFeedback.class);
        util.exportExcel(response, list, "意见反馈数据");
    }

    /**
     * 获取意见反馈详细信息
     */
    @PreAuthorize(hasPermi = "customer:feedback:query")
    @GetMapping(value = "/{feedbackId}")
    public AjaxResult getInfo(@PathVariable("feedbackId") Long feedbackId)
    {
        return AjaxResult.success(commonFeedbackService.selectCommonFeedbackById(feedbackId));
    }

    /**
     * 新增意见反馈
     */
    @PreAuthorize(hasPermi = "customer:feedback:add")
    @Log(title = "意见反馈", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommonFeedback commonFeedback)
    {
        return toAjax(commonFeedbackService.insertCommonFeedback(commonFeedback));
    }

    /**
     * 修改意见反馈
     */
    @PreAuthorize(hasPermi = "customer:feedback:edit")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonFeedback commonFeedback)
    {
        return toAjax(commonFeedbackService.updateCommonFeedback(commonFeedback));
    }

    /**
     * 删除意见反馈
     */
    @PreAuthorize(hasPermi = "customer:feedback:remove")
    @Log(title = "意见反馈", businessType = BusinessType.DELETE)
	@DeleteMapping("/{feedbackIds}")
    public AjaxResult remove(@PathVariable Long[] feedbackIds)
    {
        return toAjax(commonFeedbackService.deleteCommonFeedbackByIds(feedbackIds));
    }

    /**
     * 写新反馈
     * */
    //@SignatureValidation
    @PostMapping("/insertFeedbackInformation")
    public AjaxResult insertFeedbackInformation(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        String feedbackContent = map.get("feedbackContent").toString();
        String phoneNumber = map.get("phoneNumber").toString();
        int i = commonFeedbackService.insertCommonFeedbackByFeedbackId(customerId, feedbackContent, phoneNumber);
        if (i > 0){
            return AjaxResult.success("200","成功");
        }
        else {
            return AjaxResult.error("500", "失败");
        }
    }

    /**
     * 查看历史
     * */
    //@SignatureValidation
    @PostMapping("/viewHistoricalFeedback")
    public AjaxResult viewHistoricalFeedback(HttpServletRequest request){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(commonFeedbackService.selectCommonFeedbackByCustomerId(customerId));
    }
}
